import { BorderType } from "./borderType";
import { BoxFilterOptions, boxFilterImageData } from "./boxFilterImageData";
import { boxFilterUint8 } from "./boxFilterUint8";

/**
 * 批量处理ImageData
 * @param imageData 输入的ImageData
 * @param optionsList 滤波选项列表
 * @returns 滤波结果数组
 */
export function batchBoxFilter(
  imageData: ImageData,
  optionsList: BoxFilterOptions[],
): ImageData[] {
  return optionsList.map((options) => boxFilterImageData(imageData, options));
}

/**
 * 批量处理Uint8ClampedArray
 * 优化内存使用，共享临时缓冲区
 * @param data 输入像素数据
 * @param width 图像宽度
 * @param height 图像高度
 * @param radii 半径数组
 * @param borderType 边界处理方式
 * @returns 滤波结果数组
 */
export function batchBoxFilterUint8(
  data: Uint8ClampedArray,
  width: number,
  height: number,
  radii: number[],
  borderType: BorderType = BorderType.REPLICATE,
): Uint8ClampedArray[] {
  const results: Uint8ClampedArray[] = [];
  const size = width * height * 4;
  const tempBuffer = new Uint8ClampedArray(size); // 共享临时缓冲区

  for (const radius of radii) {
    if (radius <= 0) {
      results.push(new Uint8ClampedArray(data));
      continue;
    }

    // 复制原始数据
    const input = new Uint8ClampedArray(data);

    // 直接调用内部函数以复用临时缓冲区
    // （避免boxFilterUint8内部创建新的临时缓冲区）
    const horizontalPass = require("./boxFilterUint8").horizontalPass;
    const verticalPass = require("./boxFilterUint8").verticalPass;

    horizontalPass(input, tempBuffer, width, height, radius, borderType);
    verticalPass(tempBuffer, input, width, height, radius, borderType);

    results.push(input);
  }

  return results;
}
